package 反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Test06 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException {
        Class c1 = Class.forName("反射.User");
        //获取类的名字
        System.out.println(c1.getName());//获取包名+类名
        System.out.println(c1.getSimpleName());//获取类名
        //获取类的属性
        System.out.println("===================================================");
        Field[] fields = c1.getFields();//只能找到public属性
        System.out.println("获取public属性：");
        for (Field field : fields) {
            System.out.println(field);
        }
        Field[] declaredFields = c1.getDeclaredFields();//获取全部属性
        System.out.println("获取全部属性：");
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
        //获取指定属性的值
        Field name = c1.getDeclaredField("name");
        System.out.println(name);
        //获取类的方法
        System.out.println("==================================================");
        Method[] methods = c1.getMethods();//获取本类及其父类的全部public方法
        System.out.println("获取本类及其父类的全部public方法");
        for (Method method : methods) {
            System.out.println(method);
        }
        Method[] declaredMethods = c1.getDeclaredMethods();//获取本类的所有方法
        System.out.println("获取本类的所有方法");
        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod);
        }
        //获取指定方法
        //重载
        System.out.println("获取指定的方法：");
        Method getName = c1.getMethod("getName", null);
        Method setName = c1.getMethod("setName", String.class);
        System.out.println(getName);
        System.out.println(setName);
        //获得构造器
        System.out.println("获取本类的public构造方法");
        Constructor[] constructors = c1.getConstructors();//获取本类的public构造方法
        for (Constructor constructor : constructors) {
            System.out.println(constructor);
        }
        System.out.println("获取本类的所有构造方法");
        Constructor[] declaredConstructors = c1.getDeclaredConstructors();//获取本类的所有构造方法
        for (Constructor declaredConstructor : declaredConstructors) {
            System.out.println(declaredConstructor);
        }
        System.out.println("获取指定的构造方法");
        Constructor declaredConstructor = c1.getDeclaredConstructor(int.class, String.class, String.class, double.class, double.class);
        System.out.println(declaredConstructor);
    }
}
